package cn.itmantzy.product.controller;

import cn.itmantzy.basic.util.AjaxResult;
import cn.itmantzy.basic.util.PageList;
import cn.itmantzy.pet.domain.Pet;
import cn.itmantzy.product.domain.Product;
import cn.itmantzy.product.query.ProductQuery;
import cn.itmantzy.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 分页高级查询接口
     * @param query
     * @return
     */
    @PostMapping("/page")
    public PageList<Product> queryPage(@RequestBody ProductQuery query){
        return productService.queryPage(query);
    }

    /**
     * 新增方法接口
     * @param product
     * @return
     */
    @PutMapping("/save")
    public AjaxResult save(@RequestBody Product product){
        try {
            productService.add(product);
            return AjaxResult.getAjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 删除方法接口
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    /**
     * 上架接口
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            productService.onSale(ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            productService.offSale(ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    /**
     * 根据id查询服务详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Product findById(@PathVariable("id") Long id){
        return productService.queryByIdDetail(id);
    }
}
